// pages/remindBargain/remindBargain.js
const app = getApp();
import api from '../../../utils/util.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    discount: app.globalData.discount,
    navmess:{
      navtitle: "砍价提醒",
      backhide: 0,
      topheight: 1
    },
    goodlist: [],
    theWay: 0,  //配送方式（默认0）
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  // 商品选择
  selectclick:function(e){
    var index = parseInt(e.currentTarget.dataset.index),
        goodlist = this.data.goodlist;
    goodlist.selected = !goodlist.selected;
    this.setData({
      goodlist: goodlist
    })
  },
  // 选择配送方式
  listenerRadioGroup: function (e) {
    console.log(e.detail.value)
    this.setData({
      theWay: e.detail.value
    })
  },
  // 监听编辑信息
  gettextarea: function(e){
    this.setData({
      remark: e.detail.value
    })
  },
  usernametxt: function(e){
    this.setData({
      username: e.detail.value
    })
  },
  phonetxt: function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  // 开始砍价
  startBargain: function (e) {
    var url = e.currentTarget.dataset.url,
      goodlist = this.data.goodlist,
      token = wx.getStorageSync('token'),
      bargainId = this.data.goodlist.id,
      type = this.data.theWay,
      receiveSmallArea = this.data.receiveSmallArea,
      receiveAddress = this.data.receiveAddress,
      userName = this.data.userName,
      phone = this.data.phone;
    if (goodlist.selected) {
      // 产品类小区id必填
      if (goodlist.goodsType == 1) {
        if (this.receiveSmallArea) {
          api.post("/mini/my/bargain/bargainBegin", {
            token: token,
            receiveSmallArea: receiveSmallArea,
            receiveAddress: receiveAddress,
            userName: userName,
            phone: phone,
            type: type,
            bargainId: bargainId
          }).then(res => {
            if (res.code == 200) {
              console.log(res.result);
              wx.navigateTo({
                url: url + "?bargainOrderId=" + res.result.bargainOrderId,
              })
            }
          })
        }else{
          wx.showToast({
            title: '请选择收货地址',
            icon: "none",
            duration: 1500
          })
        }
      }else{
        console.log(bargainId,type,token)
        api.post("/mini/my/bargain/bargainBegin", {
          token: token,
          // receiveSmallArea: receiveSmallArea,
          // receiveAddress: receiveAddress,
          // userName: userName,
          // phone: phone,
          type: type,
          bargainId: bargainId
        }).then(res => {
          if (res.code == 200) {
            wx.navigateTo({
              url: url + "?bargainOrderId=" + res.result.bargainOrderId,
            })
          }
        }).catch(res => {
          console.log(res);
          wx.showToast({
            title: res.message,
            icon: "none",
            duration: 1500
          })
        })
      }
    }else{
      wx.showToast({
        title: '请选择商品',
        icon: "none",
        duration: 1500
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("activityNo",options.activityNo)
    var that = this,
        token = wx.getStorageSync('token');
    that.setData({
      token: token
    })
    // 砍价商品信息
    api.post("/mini/my/bargain/remind",{
      "token": token,
      "activityNo": options.activityNo
    }).then(res => {
      console.log(res)
      if(res.code == 200){
        that.setData({
          goodlist: res.result
        })
      }
    })
    // 取默认收货信息
    api.post("/mini/my/basic/logistics",{
      "token": token
    }).then(res => {
      console.log("收货地址列表",res)
      for(var i=0;i<res.result.length;i++){
        if(res.result[i].defaulted){
          that.setData({
            userName: res.result[i].receiver,
            phone: res.result[i].receiveMobile,
            receiveSmallArea: res.result[i].id,
            receiveAddress: res.result[i].receiveAddress,
            adress: res.result[i].receiveAddress.join(' ')  //详细地址数组
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})